<?php namespace App\Http\Controllers\Api;

use App\Files;
use App\Http\Controllers\Controller;
use App\Http\Services\Tools;
use App\Http\Services\Wechat\config;
use App\Http\Services\Wechat\WechatLogin;
use App\Images;
use Illuminate\Http\Request;
use Storage;

class WechatController extends Controller
{
    public function getJsApiPacket(Request $request){
        $info = WechatLogin::getTicketByAccessToken();
        $jsapiTicket = $info->getTicket();

        $nonceStr = WechatLogin::createNonceStr();

        $timestamp = time();

        $url = urldecode($request->get('url'));

        $config = new config();
        $appid  = $config->getAppid();

        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $appid,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$signPackage]);
    }

    public function packTest(Request $request){
        $openId = $request->get('open_id');
        $amount = $request->get('amount');
        $b = $request->get('b');

        $aa = self::sendRedPackage($openId,$amount,$b);

        var_dump($aa);
    }

    public static function sendRedPackage($openid,$money){
        $app    = app("wechat");
        $luckyMoney = $app->lucky_money;

        $luckyMoneyData = [
            'mch_billno'       => Tools::makeBillNum(),
            'send_name'        => 'ZoomYoung个性商城',
            're_openid'        => $openid,
            'total_num'        => 1,  //固定为1，可不传
            'total_amount'     => $money * 100,  //单位为分，不小于300
            'wishing'          => '恭喜您获得返现红包，感谢您对ZoomYoung的支持',
            'act_name'         => 'ZoomYoung返现活动',
            'remark'           => '测试备注',
            'scene_id'         => 'PRODUCT_1'
        ];
        $result = $luckyMoney->sendNormal($luckyMoneyData);

        return $result;
    }

    public static function uploadMaterialTemporary($imgId){
        /** @var Images $thatImg */
        $thatImg = Images::whereId($imgId)->first();
        if(!$thatImg)
            return false;

        /** @var Files $file */
        $file = $thatImg->files;
        if(!$file)
            return false;

        $toDirPath = "tmp";
        $toFilePath = $toDirPath.'/'.$file->file_md5.$file->file_type;

        $content = $file->getContent();

        if(!Storage::disk()->exists($toDirPath)){
            Storage::disk()->makeDirectory($toDirPath);
        }

        if(!Storage::disk()->exists($toFilePath)){
            Storage::disk()->put($toFilePath,$content);
        }

        $app    = app("wechat");
        // 临时素材
        $temporary = $app->material_temporary;

        $imgLocalPath = storage_path().'/app/tmp/'.$file->file_md5.$file->file_type;
        $result = $temporary->uploadImage($imgLocalPath);

        if($result){
            Storage::disk()->delete($toFilePath);
            return $result['media_id'];
        }else{
            return false;
        }

    }
}